package com.luge.model.request;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.luge.base.Base;
import com.luge.model.KeyItem;
import com.luge.model.KeyList;
import com.luge.model.SongDetails;
import com.luge.model.SongItem;
import com.luge.model.SongList;
import com.luge.utils.Helper;
import com.luge.utils.StreamTool;

public class ParseJSON {
	private static final int CODE_IS_TRUE = 1;
	private static final int CODE_IS_FALSE = 0;
	private static final String RESULT_CODE = "resultcode";
	public static final String RESULT = "result";
	private static final String ERROR_INFO = "errorInfo";

	public static Object request(Task task) {
		String url = task.getUrl();
		Helper.log(url);
		switch (task.getId()) {
		case Task.LOGIN:
			return ParseJSON.loginValidate(url);
		case Task.KEY_LIST:
			return ParseJSON.getKeyList(url);
		case Task.SONG_LIST:
			return ParseJSON.getSongList(url);
		case Task.SONG_DETAILS:
			return ParseJSON.getSongDetails(url);
		default:
			break;
		}
		return null;
	}

	private static BaseModel loginValidate(String uRL) {
		BaseModel model = null;
		try {
			JSONObject jsonObject = getJSONPullParser(uRL);
			int resultCode = jsonObject.getInt(RESULT_CODE);
			model = new BaseModel();
			if (resultCode == CODE_IS_TRUE) {
				model.setCode(resultCode);
				Base.key = jsonObject.getString(RESULT);
			} else if (resultCode == CODE_IS_FALSE) {
				model.setErrorInfo(jsonObject.getString(ERROR_INFO));
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			return null;
		}
		return model;
	}

	private static KeyList getKeyList(String uRL) {
		KeyList model = null;
		try {
			JSONObject jsonObject = getJSONPullParser(uRL);
			int resultCode = jsonObject.getInt(RESULT_CODE);
			model = new KeyList();
			if (resultCode == CODE_IS_TRUE) {
				model.setCode(resultCode);
				JSONArray array = jsonObject.getJSONArray(RESULT);
				ArrayList<KeyItem> items = new ArrayList<KeyItem>();
				for (int i = 0; i < array.length(); i++) {
					KeyItem item = new KeyItem();
					JSONObject jsonObj = array.getJSONObject(i);
					item.setTitle(Helper.decodeUTF8(jsonObj
							.getString("actionname")));
					item.setImgUrl(jsonObj.getString("imgurl"));
					items.add(item);
				}
				model.setList(items);
			} else if (resultCode == CODE_IS_FALSE) {
				model.setErrorInfo(jsonObject.getString(ERROR_INFO));
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			return null;
		}
		return model;
	}

	private static SongList getSongList(String uRL) {
		SongList model = null;
		try {
			JSONObject jsonObject = getJSONPullParser(uRL);
			int resultCode = jsonObject.getInt(RESULT_CODE);
			model = new SongList();
			if (resultCode == CODE_IS_TRUE) {
				model.setCode(resultCode);
				JSONObject result = jsonObject.getJSONObject(RESULT);
				model.setBigImageUrl(result.getString("actionbigimg"));
				JSONArray array = result.getJSONArray("data");
				ArrayList<SongItem> items = new ArrayList<SongItem>();
				for (int i = 0; i < array.length(); i++) {
					SongItem item = new SongItem();
					JSONObject jsonObj = array.getJSONObject(i);
					item.setId(jsonObj.getString("songid"));
					item.setImgUrl(jsonObj.getString("imgurl"));
					item.setName(Helper.decodeUTF8(jsonObj
							.getString("songname")));
					item.setSinger(Helper.decodeUTF8(jsonObj
							.getString("singer")));
					item.setInfo(Helper.decodeUTF8(jsonObj.getString("info")));
					items.add(item);
				}
				model.setList(items);
			} else if (resultCode == CODE_IS_FALSE) {
				model.setErrorInfo(jsonObject.getString(ERROR_INFO));
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			return null;
		}
		return model;
	}

	private static SongDetails getSongDetails(String uRL) {
		SongDetails model = null;
		try {
			JSONObject jsonObject = getJSONPullParser(uRL);
			int resultCode = jsonObject.getInt(RESULT_CODE);
			model = new SongDetails();
			if (resultCode == CODE_IS_TRUE) {
				JSONObject result = jsonObject.getJSONObject(RESULT);
				model.setCode(resultCode);
				model.setSearchUrl(result.getString("searchurl"));
				model.setSrcPath(result.getString("songurl"));
				model.setBigIcon(result.getString("songimgbigurl"));
				model.setTypeStr(Helper.decodeUTF8(result.getString("songtype")));
				model.setWords(Helper.decodeUTF8(result.getString("songwords")));
				model.setNum(result.getString("givenum"));
			} else if (resultCode == CODE_IS_FALSE) {
				model.setErrorInfo(jsonObject.getString(ERROR_INFO));
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			return null;
		}
		return model;
	}

	private static JSONObject getJSONPullParser(String urlStr) throws Exception {
		InputStream inputStream = StreamTool.getInputStream(urlStr);
		StringBuilder data = new StringBuilder();
		BufferedReader br = new BufferedReader(new InputStreamReader(
				inputStream, "GB2312"));
		for (String s = br.readLine(); s != null; s = br.readLine()) {
			data.append(s);
		}
		if (null != inputStream) {
			inputStream.close();
			inputStream = null;
		}
		if (null != br) {
			br.close();
			br = null;
		}
		Helper.log(data.toString());
		return (JSONObject) new JSONTokener(data.toString()).nextValue();
	}
}
